﻿using System;
using System.Collections.Generic;
using System.Linq;
using Shop.Base.Entity;
using Shop.Base.Entity.Delete;
using Shop.Base.Entity.Insert;
using Shop.Base.Entity.Update;
using Shop.Base.Interface;
using Shop.Entity;
using Shop.Server;
using Shop.Tools;

namespace Shop.Base.Server
{
    public class SystemButton:BaseServer<SystemButtonPo> ,ISystemButton
    {
        public bool Update(UpdateSystemButton t, ComUser com)
        {
            t.UpdateAt = DateTime.Now;
            t.UpdateId = com.Id;
            t.UpdateName = com.UserName;
            var all = GetAll<SystemButtonPo>();
            if (t.ButtonNum == 0 || all.Any(x => x.IsDel == false && x.ButtonNum == t.ButtonNum && x.Id!=t.Id))
                t.ButtonNum = all.Where(x => x.IsDel == false).Max(x => x.ButtonNum) + 1;
            return db.Updateable(t).ExecuteCommand() > 0;
        }

        public bool Insert(AddSystemButton t, ComUser com)
        {
            t.CreateId = com.Id;
            t.CreateName = com.UserName;
            var all = GetAll<SystemButtonPo>();
            if (t.ButtonNum == 0 || all.Any(x => x.IsDel == false && x.ButtonNum == t.ButtonNum))
                t.ButtonNum = all.Where(x => x.IsDel == false).Max(x => x.ButtonNum) + 1;
            return db.Insertable(t).ExecuteCommand() > 0;
        } 

        public List<SystemButtonPo> GetPage(string buttonName, int isDel, int pageIndex, int pageSize, out int total)
        {
            total = 0;
            return db.Queryable<SystemButtonPo>().WhereIF(!buttonName.IsNullOrEmpty(), $" ButtonName like @ButtonName ", new { ButtonName = $"%{buttonName}%" })
                .OrderBy("ButtonNum").ToPageList(pageIndex, pageSize, ref total);
        }
    }
}